package com.uyaer.wheel.plus
{
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import com.uyaer.wheel.common.Dir;

	public class Keybord
	{
		private var stage:Stage;
		private static var m_instance:Keybord;
		private var isRuning:Boolean = false;
		public function Keybord()
		{
		}
		
		public static function get instance():Keybord{
			return m_instance ||= new Keybord(); 
		}
		
		public function start(stage:Stage):void{
			if(isRuning){
				stop();
			}
			this.stage = stage;
			stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP,onKeyHandler);
			isRuning = true;
		}
		
		public function stop():void{
			stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyHandler);
			stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyHandler);
			isRuning = false;
		}
		
		private function onKeyHandler(e:KeyboardEvent):void{
			if(e.type == KeyboardEvent.KEY_DOWN){
				switch(e.keyCode){
					case Keyboard.LEFT:
						Dir.dirX = -0.3;
						break;
					case Keyboard.RIGHT:
						Dir.dirX = 0.3;
						break;
					case Keyboard.UP:
						Dir.dirY = -0.3;
						break;
					case Keyboard.DOWN:
						Dir.dirY = 0.3;
						break;
				}
			}else{
				switch(e.keyCode){
					case Keyboard.LEFT:
						if(Dir.dirX<0)Dir.dirX = 0;
						break;
					case Keyboard.RIGHT:
						if(Dir.dirX>0)Dir.dirX = 0;
						break;
					case Keyboard.UP:
						if(Dir.dirY<0)Dir.dirY = 0;
						break;
					case Keyboard.DOWN:
						if(Dir.dirY>0)Dir.dirY = 0;
						break;
				}
			}
		}
	}
}